<template>
  <el-dialog title="分配角色" :visible="showRoleDialog" @close="btnCancel">
    <!-- 多选框组 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- 定义footer的插槽 -->
    <template #footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employess'
import { getUserDetailById } from '@/api/user'

export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    userId: {
      // 用户的id 当前要处理的哪个用户
      type: String,
      default: null
    //   require: true
    }
  },
  data() {
    return {
      roleIds: [], // 选中角色列表
      list: [] // 所有角色列表
    }
  },
  created() {
    this.getRoleList() // 获取所有角色
  },
  methods: {
    btnCancel() {
      this.roleIds = [] // 清空原来的数据
      this.$emit('update:showRoleDialog', false)
    },
    async btnOK() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds })
      this.$message.success('分配成功')
      this.$emit('update:showRoleDialog', false)
    },
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      this.roleIds = roleIds // 赋值本用户的角色
    },
    async getRoleList() {
      const { rows } = await getRoleList()
      this.list = rows
      console.log(rows)
    }
  }
}
</script>

<style>

</style>
